import { defineStore } from "pinia";
import {
    getLocalUserInfo,
    getToken,
    removeToken,
    setLocalUserInfo,
    removeLocalUserInfo,
    setToken,
} from "@/utils/storage.js";
import api from "@/api";

export const useUserStore = defineStore("user", {
    state: () => {
        return {
            isLogin: false,
            token: getToken(),
            oUserInfo: getLocalUserInfo(),
        };
    },
    actions: {
        // 初始化服务器公钥
        loadIsLogin() {
            this.isLogin = this.token == null;
        },
        // todo 获取用户信息
        loadUserInfo() {
            return this.oUserInfo;
        },
        // todo 手机号登录
        loginByPhone(data) {
            return new Promise(async (resolve, reject) => {
                let res = await api.login.login(data);
                this.isLogin = true;
                this.token = res.accessToken;
                setToken(this.token);
                this.oUserInfo = res;
                setLocalUserInfo(this.oUserInfo);
                resolve(res);
                // resolvsetLocalUserInfoe(true);
            });
        },
        // todo 微信手机号登录
        loginByWxPhone(data) {
            return new Promise(async (resolve, reject) => {
                let res = await api.login.loginWxPhone(data);
                this.isLogin = true;
                this.token = res.accessToken;
                setToken(this.token);
                this.oUserInfo = res;
                setLocalUserInfo(this.oUserInfo);
                resolve(res);
                // resolvsetLocalUserInfoe(true);
            });
        },
        // todo 微信登录
        loginByWeChat() {
            return new Promise((resolve) => {
                uni.login({
                    provider: "weixin", //使用微信登录
                    success: async (loginRes) => {
                        let data = await api.user.login({
                            code: loginRes.code,
                        });
                        this.isLogin = true;
                        this.token = data.accessToken;
                        setToken(this.token);
                        resolve(true);
                    },
                    fail(err) {
                        console.error("uni login fail", err);
                        reject();
                    },
                });
            });
        },
        // 获取用户信息
        getLoginUserInfo() {
            return new Promise(async (resolve) => {
                let data = await api.login.getUserInfo();
                this.oUserInfo = data;
                setLocalUserInfo(this.oUserInfo);
                resolve(this.oUserInfo);
            });
        },
        // 退出
        logout() {
            return new Promise(async (resolve) => {
                await api.user.logout();
                this.isLogin = false;
                this.token = "";
                this.oUserInfo = null;
                removeLocalUserInfo();
                removeToken();
                resolve(true);
            });
        },
        removeLoginInfo() {
            return new Promise((resolve) => {
                this.isLogin = false;
                this.token = "";
                this.oUserInfo = null;
                removeLocalUserInfo();
                removeToken();
                resolve();
            });
        },
    },
});
